package com.ruoyi.quartz.task;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.store.domain.Payslip;
import com.ruoyi.store.domain.TotalMoney;
import com.ruoyi.store.service.IPayslipService;
import com.ruoyi.store.service.ITotalMoneyService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component("totalMoneyCalTask")
public class TotalMoneyCalTask {

    @Autowired
    private ITotalMoneyService totalMoneyService;
    @Autowired
    private IPayslipService payslipService;

    public void calTotalMoney() {
        Payslip payParam = new Payslip();
        payParam.setYear(String.valueOf(DateUtil.year(new Date())));
        payParam.setMonth(String.valueOf(DateUtil.month(new Date())));
        List<Payslip> list = payslipService.selectPayslipList(payParam);
        if (CollectionUtils.isNotEmpty(list)) {
            BigDecimal totalGet = list.stream().map(e -> e.getTotalGet()).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal totalwages = list.stream().map(e -> e.getWages()).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal totalCash = list.stream().map(e -> e.getCash()).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal totalRecharge = list.stream().map(e -> e.getRecharge()).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal totalWorkMore = list.stream().map(e -> e.getWorkMore()).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal totalMember = list.stream().map(e -> e.getMember()).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal totalMemberMore = list.stream().map(e -> e.getMemberMore()).reduce(BigDecimal.ZERO, BigDecimal::add);
            TotalMoney totalParam = new TotalMoney();
            totalParam.setYear(payParam.getYear());
            totalParam.setMonth(payParam.getMonth());
            List<TotalMoney> totalList = totalMoneyService.selectTotalMoneyList(totalParam);
            if(CollectionUtils.isEmpty(totalList)){
                TotalMoney saveVo= new TotalMoney();
                saveVo.setYear(payParam.getYear());
                saveVo.setMonth(payParam.getMonth());
                saveVo.setCash(totalCash);
                saveVo.setRecharge(totalRecharge);
                saveVo.setWorkMore(totalWorkMore);
                saveVo.setMember(totalMember);
                saveVo.setMemberMore(totalMemberMore);
                saveVo.setTotalMoney(totalGet);
                saveVo.setWages(totalwages);
                saveVo.setProfit(saveVo.getTotalMoney().subtract(saveVo.getWages()));
                saveVo.setSisiMoney(saveVo.getProfit().multiply(new BigDecimal(0.85)));
                saveVo.setMlMoney(saveVo.getProfit().subtract(saveVo.getSisiMoney()));
                totalMoneyService.insertTotalMoney(saveVo);
            }else{
                TotalMoney saveVo=totalList.get(0);
                saveVo.setCash(totalCash);
                saveVo.setRecharge(totalRecharge);
                saveVo.setWorkMore(totalWorkMore);
                saveVo.setMember(totalMember);
                saveVo.setMemberMore(totalMemberMore);
                saveVo.setTotalMoney(totalGet);
                saveVo.setWages(totalwages);
                saveVo.setProfit(saveVo.getTotalMoney().subtract(saveVo.getWages()).subtract(saveVo.getPayOut()));
                saveVo.setSisiMoney(saveVo.getProfit().multiply(new BigDecimal(0.85)));
                saveVo.setMlMoney(saveVo.getProfit().subtract(saveVo.getSisiMoney()));
                totalMoneyService.updateTotalMoney(saveVo);
            }
        }

    }
}
